package cn.pzaily.orange.manage.controller;

import cn.pzaily.orange.core.utils.ObjectUtils;
import cn.pzaily.orange.core.utils.StringUtils;
import cn.pzaily.orange.core.utils.http.AppHttpCodeEnum;
import cn.pzaily.orange.core.utils.http.ResponseResult;
import cn.pzaily.orange.manage.constant.SysConstants;
import cn.pzaily.orange.manage.model.SysUser;
import cn.pzaily.orange.manage.model.vo.LoginUserVO;
import cn.pzaily.orange.manage.service.SysUserLoginService;
import cn.pzaily.orange.manage.service.SysUserService;
import cn.pzaily.orange.manage.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: cn.pzaily.orange.controller
 * @Description:
 * @Author: Pang
 * @Date: 2021-05-08 23:08
 */
@RestController
public class SysLoginController {

    @Autowired
    private SysUserService userServer;

    @Autowired
    private SysUserLoginService userLoginServer;

    /**
     * 欢迎界面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseResult welcome() {
        return ResponseResult.okResult("welcome to orange");
    }

    /**
     * 获取验证码
     *
     * @param response
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public ResponseResult captcha() throws IOException {
        return ResponseResult.okResult(userLoginServer.getCaptcha());
    }

    /**
     * 登录操作
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseResult login(@RequestBody LoginUserVO user) {
        String username = user.getUsername();
        String password = user.getPassword();
        String captchaKey = user.getCaptchaKey();
        String captchaValue = user.getCaptchaValue();

        // 用户名密码或验证码为空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(captchaKey) || StringUtils.isEmpty(captchaValue)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USERNAME_PASSWORD_NOT_EXIST);
        }

        // 校验验证码
        if (!userLoginServer.validationCaptcha(captchaKey, captchaValue)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CODE_ERROR);
        }


        SysUser byUserName = userServer.findByUserName(username);
        // 用户是否存在
        if (ObjectUtils.isNull(byUserName)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        // 校验密码是否正确
        if (!SecurityUtils.matchesPassword(password, byUserName.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        // 校验用户是否锁定
        if (byUserName.getStatus() == SysConstants.LOCKED_STATUS) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_LOCKED);
        }

        String token = userLoginServer.login(username, password);
        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        return ResponseResult.okResult(result);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseResult register(@RequestBody SysUser user) {
        String name = user.getName();
        String password = user.getPassword();
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USERNAME_PASSWORD_NOT_EXIST);
        }
        SysUser byUserName = userServer.findByUserName(name);
        if (ObjectUtils.isNotNull(byUserName)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NAME_EXIST);
        }

        if (userServer.save(user)) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


}
